/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Command;
import com.sun.lwuit.Label;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BoxLayout;
import core.ScreenEngine;
import view.additional.CellItem;
import view.additional.MenuList;
import view.commands.BackCommand;

/**
 *
 * @author Kvest
 */
public class TimeSelectForm extends BasicForm implements MenuList.MenuListSelection
{
    private static final int OK_COMMAND_ID = 1;
    
    private MenuList items;
    
    private TimeSelected timeSelectedListener;
    
    public TimeSelectForm(String title, TimeSelected timeSelectedListener)
    {
        super(title);
        
        this.timeSelectedListener = timeSelectedListener;
        
        init();
    }
    
    private void init()
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        setScrollable(false);
        
        String min = (new Label("min")).getText();
        items = new MenuList();
        items.addItem(new CellItem(5 ,"5 " + min));
        items.addItem(new CellItem(10 ,"10 " + min));
        items.addItem(new CellItem(15 ,"15 " + min));;
        items.setSelectionListener(this);
        addComponent(items);
        
        //Команды
        addCommand(new BackCommand());
        
        Command ok = new Command("ok", OK_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
                itemSelected(items.getSelectedId());
                
                evt.consume();
            }
        };
        addCommand(ok);
    }
    
    public void itemSelected(int id)
    {
        //Выходим на форму назад
        ScreenEngine.getEngine().pop(true);
            
        if (id > 0)
        {
            //Отстукиваемся с выбранным временем
            if (timeSelectedListener != null)
            {
                timeSelectedListener.timeSelected(id);
            }
        }
    }
    
    public void show()
    {
        items.setSelectedIndex(0);
        
        super.show();
    }
    
    public interface TimeSelected
    {
        public void timeSelected(int time);
    }
}
